﻿using System;
using System.IO;
//简单实现写文件日志
namespace CommServer.Common
{
    public class Log
    {
        private static string m_Path = Directory.GetCurrentDirectory() + "\\LogFiles";
        private static object m_SynObject = new object();

        private static int m_LogInfo = 0;
        private static int m_LogDebug = 0;
        private static int m_LogConsole = 0;

        static Log()
        {
            try
            {
                if (!Directory.Exists(m_Path))
                {
                    Directory.CreateDirectory(m_Path);
                }
                m_LogInfo = Config.GetIntValue("LogInfo");
                m_LogDebug = Config.GetIntValue("LogDebug");
                m_LogConsole = Config.GetIntValue("LogConsole");
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("read config file error:" + ex.Message);
            }
        }

        public static void SetOptions(int LogInfo, int LogDebug, int LogConsole)
        {
            m_LogInfo = LogInfo;
            m_LogDebug = LogDebug;
            m_LogConsole = LogConsole;
        }
        //每个设备每天一个文件
        public static void Info(string DevAddr, string v)
        {
            if (m_LogInfo != 1)
            {
                return;
            }

            lock (m_SynObject)
            {
                try
                {
                    string FilePath = m_Path + "\\" + DevAddr;
                    if (!Directory.Exists(FilePath))
                    {
                        Directory.CreateDirectory(FilePath);
                    }

                    string FileName = $"{FilePath}\\{DateTime.Now.ToString("yyyy-MM-dd")}";
                    File.AppendAllText(FileName, $"INFO {DateTime.Now.ToString("HH:mm:ss")} {v}\r\n");
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine("write log device info error:" + ex.Message);
                }
            }
        }
        //每天一个文件
        public static void Info(string v)
        {
            if (m_LogInfo != 1)
            {
                return;
            }
            try
            {
                lock (m_SynObject)
                {
                    string FileName = $"{m_Path}\\{DateTime.Now.ToString("yyyy-MM-dd")}";
                    File.AppendAllText(FileName, $"INFO {DateTime.Now.ToString("HH:mm:ss")} {v}\r\n");
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("write log device info error:" + ex.Message);
            }           
        }

        public static void Debug(string v)
        {
            if (m_LogDebug != 1)
            {
                return;
            }
            try
            {
                lock (m_SynObject)
                {
                    string FileName = $"{m_Path}\\debug\\{DateTime.Now.ToString("yyyy-MM-dd")}";
                    File.AppendAllText(FileName, $"DEBUG {DateTime.Now.ToString("HH:mm:ss.fff")} {v}\r\n");
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("write log device info error:" + ex.Message);
            }
        }

        public static void Console(string v)
        {

            System.Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + ":" + v);
        }

        public static void Error(string v)
        {
            try
            {
                lock (m_SynObject)
                {
                    string FileName = $"{m_Path}\\{DateTime.Now.ToString("yyyy-MM-dd")}";
                    string Content = $"ERROR {DateTime.Now.ToString("HH:mm:ss")} {v}\r\n";
                    File.AppendAllText(FileName, Content);
                    System.Console.WriteLine(Content);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("write log device info error:" + ex.Message);
            }
        }
        
        public static void Warn(string v)
        {
            try
            {
                lock (m_SynObject)
                {
                    string FileName = $"{m_Path}\\{DateTime.Now.ToString("yyyy-MM-dd")}";
                    File.AppendAllText(FileName, $"WARN {DateTime.Now.ToString("HH:mm:ss")} {v}\r\n");
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("write log device info error:" + ex.Message);
            }
        }
    }
}
